package com.sunda.spmsweb.wmscontroller;

import com.alibaba.fastjson.JSONObject;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsweb.util.JWTUtil;
import com.sunda.spmswms.service.IWhsOtherTaskCancelService;
import com.sunda.spmswms.service.IWhsOtherTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/whsOther")
@Api(tags = "其它出入库接口")
public class WhsOtherController {

    @Autowired
    IWhsOtherTaskService iWhsOtherTaskService;

    @Autowired
    IWhsOtherTaskCancelService iWhsOtherTaskCancelService;

    @RequestMapping("/updateWhsOut")
    @ApiOperation(value = "其它出入库创建、更新接口", notes = "其它出入库创建、更新接口\n" +
            "{\n" +
            "  \"whsOther\": {\n" +
            "    \"updateTime\": \"\",\n" +
            "    \"operateUser\": \"\",\n" +
            "    \"createTime\": \"\",\n" +
            "    \"costCenter\": \"\",\n" +
            "    \"createDate\": \"2021-06-25\",\n" +
            "    \"createUserid\": \"999904\",\n" +
            "    \"deliveryNoteSrm\": \"\",\n" +
            "    \"documentDate\": \"\",\n" +
            "    \"goodsType\": \"M\",\n" +
            "    \"movementType\": \"311\",\n" +
            "    \"packageList\": \"\",\n" +
            "    \"packageNote\": \"\",\n" +
            "    \"postingDate\": \"\",\n" +
            "    \"purchaseGroup\": \"\",\n" +
            "    \"purchaseGroupName\": \"\",\n" +
            "    \"relatedDocumentDate\": \"\",\n" +
            "    \"relatedPostingDate\": \"\",\n" +
            "    \"relatedTaskId\": \"\",\n" +
            "    \"relatedVoucherNumber\": \"\",\n" +
            "    \"relatedVoucherYear\": \"\",\n" +
            "    \"remarks\": \"\",\n" +
            "    \"sapOrderType\": \"\",\n" +
            "    \"sapPo\": \"\",\n" +
            "    \"sapVoucherNumber\": \"\",\n" +
            "    \"sapVoucherYear\": \"\",\n" +
            "    \"spmsId\": \"\",\n" +
            "    \"taskStatus\": \"1\",\n" +
            "    \"uuid\": \"\",\n" +
            "    \"werks\": \"CN01\",\n" +
            "    \"whsLocationCode\": \"1061\",\n" +
            "    \"goodsTypeDesc\": \"贸易货\",\n" +
            "    \"movementTypeDesc\": \"卸货入库\",\n" +
            "    \"taskStatusDesc\": \"草稿\",\n" +
            "    \"currentStatus\": \"1\",\n" +
            "    \"targetStatus\": \"1\"\n" +
            "  },\n" +
            "  \"whsOtherDtl\": [\n" +
            "    {\n" +
            "      \"rowId\": 1624592938262,\n" +
            "      \"autoId\": 1624592938263,\n" +
            "      \"materialNo\": \"180002671\",\n" +
            "      \"packageList\": \"PL00027372\",\n" +
            "      \"packageRowId\": \"10\",\n" +
            "      \"basicUnit\": \"PCS\",\n" +
            "      \"operateQty\": 100,\n" +
            "      \"packageUnit\": \"PAC\",\n" +
            "      \"packageQty\": 8.333,\n" +
            "      \"materialNoIn\": \"\",\n" +
            "      \"qtyIn\": \"\",\n" +
            "      \"materialNoOut\": \"\",\n" +
            "      \"qtyOut\": \"\",\n" +
            "      \"costCenter\": \"\",\n" +
            "      \"rowRemarks\": \"\",\n" +
            "      \"supplierDesc\": \"\",\n" +
            "      \"supplierNo\": \"\",\n" +
            "      \"tempStorage\": [\n" +
            "        {\n" +
            "          \"storageUuid\": \"cd8a479246954765bc927f992f368987\",\n" +
            "          \"qty\": 100,\n" +
            "          \"storageNo\": \"A020101\",\n" +
            "          \"unitValue\": 8.333\n" +
            "        }\n" +
            "      ],\n" +
            "      \"werks\": \"CN01\",\n" +
            "      \"whsLocationCode\": \"1061\",\n" +
            "      \"whsOtherUuid\": \"\",\n" +
            "      \"materialZhDesc\": \"方铁锹1.5KG S501 铁柄 SHOVEL S501 12PCS/BAG\",\n" +
            "      \"materialEnDesc\": \"Shovel iron-handle Square S501 W16\",\n" +
            "      \"packingSpec\": \"\",\n" +
            "      \"numerator\": 12,\n" +
            "      \"denominator\": 1,\n" +
            "      \"storageUuidList\": []\n" +
            "    },\n" +
            "    {\n" +
            "      \"rowId\": 1624592938264,\n" +
            "      \"autoId\": 1624592938265,\n" +
            "      \"materialNo\": \"180002671\",\n" +
            "      \"packageList\": \"PL00027372\",\n" +
            "      \"packageRowId\": \"20\",\n" +
            "      \"basicUnit\": \"PCS\",\n" +
            "      \"operateQty\": 3600,\n" +
            "      \"packageUnit\": \"PAC\",\n" +
            "      \"packageQty\": 300,\n" +
            "      \"materialNoIn\": \"\",\n" +
            "      \"qtyIn\": \"\",\n" +
            "      \"materialNoOut\": \"\",\n" +
            "      \"qtyOut\": \"\",\n" +
            "      \"costCenter\": \"\",\n" +
            "      \"rowRemarks\": \"\",\n" +
            "      \"supplierDesc\": \"\",\n" +
            "      \"supplierNo\": \"\",\n" +
            "      \"tempStorage\": [\n" +
            "        {\n" +
            "          \"storageUuid\": \"b95946ac1dab42b79ade3438acb0b538\",\n" +
            "          \"qty\": 1200,\n" +
            "          \"storageNo\": \"A010101\",\n" +
            "          \"unitValue\": 100\n" +
            "        },\n" +
            "        {\n" +
            "          \"storageUuid\": \"cd8a479246954765bc927f992f368987\",\n" +
            "          \"qty\": 2400,\n" +
            "          \"storageNo\": \"A020101\",\n" +
            "          \"unitValue\": 200\n" +
            "        }\n" +
            "      ],\n" +
            "      \"werks\": \"CN01\",\n" +
            "      \"whsLocationCode\": \"1061\",\n" +
            "      \"whsOtherUuid\": \"\",\n" +
            "      \"materialZhDesc\": \"方铁锹1.5KG S501 铁柄 SHOVEL S501 12PCS/BAG\",\n" +
            "      \"materialEnDesc\": \"Shovel iron-handle Square S501 W16\",\n" +
            "      \"packingSpec\": \"\",\n" +
            "      \"numerator\": 12,\n" +
            "      \"denominator\": 1,\n" +
            "      \"storageUuidList\": [\n" +
            "        {\n" +
            "          \"storageUuid\": \"b95946ac1dab42b79ade3438acb0b538\",\n" +
            "          \"qty\": 1200,\n" +
            "          \"storageNo\": \"A010101\",\n" +
            "          \"unitValue\": 100\n" +
            "        }\n" +
            "      ]\n" +
            "    }\n" +
            "  ]\n" +
            "}\n", httpMethod = "POST")
    public synchronized ResponseResult updateWhsOut(@RequestBody JSONObject WhsOtherDtl) {
        String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
        try {
            return iWhsOtherTaskService.onSaveWhsOther(WhsOtherDtl, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error().add("error", e.getMessage());
        }
    }


    @PostMapping("/getWhsOtherList")
    @ApiOperation(value = "获取其它出入库任务列表", notes = "获取其它出入库任务列表" +
            "{\n" +
            "  \"werks\":\"CN01\",\n" +
            "  \"whsLocationCode\":\"1011\",\n" +
            "  \"movementType\":\"311\",\n" +
            "  \"goodsType\":\"M\",\n" +
            "  \"createDate\":[\"2021-06-18\",\"2021-06-21\"],\n" +
            "  \"taskStatus\":\"1\",\n"+
            "  \"pageNo\": 1,\n"+
            "  \"pageSize\": 20 \n"+
            "}", httpMethod = "POST")
    public ResponseResult getWhsOtherList(@RequestBody JSONObject whsOFilter) {
        return iWhsOtherTaskService.getWhsOtherList(whsOFilter);
    }

    @GetMapping("/getWhsOtherWithDtl")
    @ApiOperation(value = "根据uuid获取其它出入库单据详情数据", notes = "根据uuid获取其它出入库单据详情数据")
    public ResponseResult getWhsOtherWithDtl(@RequestParam(required = true) String uuid) {
        return iWhsOtherTaskService.getWhsOtherWithDtl(uuid);
    }

    @GetMapping("/getSapReturnedOrder")
    @ApiOperation(value = "从SAP Rest请求退货订单信息", notes = "退货出库\n" +
            "退货出库退货订单获取接口，输入交货单号，采购订单号等信息向SAP REST请求\n" +
            "示例参数 purchaseOrderNo = 4300000007, 4500104823")
    public ResponseResult getSapReturnedOrder(String purchaseOrderNo) {
        return iWhsOtherTaskService.getSapReturnedOrder("", purchaseOrderNo);
    }

    @PostMapping("/cancelWhsOtherTask")
    @ApiOperation(value = "从SAP撤销其他出入库", notes = "从SAP撤销其他出入库\n" +
            "入参为其他出入库任务 uuid = 218aff6238bf4265978923742955fe32 \n")
    public ResponseResult cancelWhsOtherTask(@RequestParam String whsOtherUuid,
                                             @RequestParam String cancelDate,
                                             @RequestParam(required = false) String cancelRemark) {
        String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
        try {
            return iWhsOtherTaskCancelService.cancelWhsOtherTask(userId, whsOtherUuid, cancelDate, cancelRemark);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error().add("error", e.getMessage());
        }
    }

//    @GetMapping("/checkException")
//    @ApiOperation(value = "checkException异常测试", notes = "checkException")
//    public ResponseResult checkException(String aa, String bb) {
//        try {
//            return iWhsOtherTaskCancelService.checkException(aa, bb);
//        }catch (Exception e){
//            e.printStackTrace();
//            return ResponseResult.error().add("error", e.getMessage());
//        }
//    }

}
